package com.xinyan.util;

import com.xinyan.plc.RepeatParam;
import org.apache.commons.lang3.StringUtils;
import org.springframework.util.CollectionUtils;

import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class XYUtil {
    private static List<String> keyList = new ArrayList<>();

    static {
        keyList.add("startTime1");
        keyList.add("endTime1");
        keyList.add("startTime2");
        keyList.add("endTime2");
        keyList.add("startTime3");
        keyList.add("endTime3");
        keyList.add("startTime4");
        keyList.add("endTime4");
    }

    public static Map<String, String> repeatParam2Map(RepeatParam repeatParam){
        Map<String,String> map = new HashMap<>();
        map.put("repeatWeek0",repeatParam.isV5100() ? "on" : "");
        map.put("repeatWeek1",repeatParam.isV5101() ? "on" : "");
        map.put("repeatWeek2",repeatParam.isV5102() ? "on" : "");
        map.put("repeatWeek3",repeatParam.isV5103() ? "on" : "");
        map.put("repeatWeek4",repeatParam.isV5104() ? "on" : "");
        map.put("repeatWeek5",repeatParam.isV5105() ? "on" : "");
        map.put("repeatWeek6",repeatParam.isV5106() ? "on" : "");

        map.put("repeatTime1",repeatParam.isV5110() ? "on" : "");
        map.put("repeatTime2",repeatParam.isV5111() ? "on" : "");
        map.put("repeatTime3",repeatParam.isV5112() ? "on" : "");
        map.put("repeatTime4",repeatParam.isV5113() ? "on" : "");

        map.put("startTime1", getFormatstr(repeatParam.getVw512(),repeatParam.getVw528()));
        map.put("endTime1", getFormatstr(repeatParam.getVw514(),repeatParam.getVw530()));
        map.put("startTime2", getFormatstr(repeatParam.getVw516(),repeatParam.getVw532()));
        map.put("endTime2", getFormatstr(repeatParam.getVw518(),repeatParam.getVw534()));
        map.put("startTime3", getFormatstr(repeatParam.getVw520(),repeatParam.getVw536()));
        map.put("endTime3", getFormatstr(repeatParam.getVw522(),repeatParam.getVw538()));
        map.put("startTime4", getFormatstr(repeatParam.getVw524(),repeatParam.getVw540()));
        map.put("endTime4", getFormatstr(repeatParam.getVw526(),repeatParam.getVw542()));
        repeatParam.getRealVal();
        map.put("runMinute", String.valueOf(repeatParam.getVb500_0()));
        map.put("runSeconds",String.valueOf(repeatParam.getVb501_0()));
        map.put("stopMinute",String.valueOf(repeatParam.getVb502_0()));
        map.put("stopSeconds",String.valueOf(repeatParam.getVb503_0()));

        return map;
    }

    public static RepeatParam map2RepeatParam(Map<String, String> map) {
        RepeatParam repeatParam = new RepeatParam();

        String runMinute = map.get("runMinute");
        String runSeconds = map.get("runSeconds");
        String stopMinute = map.get("stopMinute");
        String stopSeconds = map.get("stopSeconds");
        // 自动模式下运行持续时间-分钟
        repeatParam.setVb500(new byte[]{StringUtils.isBlank(runMinute) ? 0 : Byte.valueOf(runMinute)});
        // 自动模式下运行持续时间-秒
        repeatParam.setVb501(new byte[]{StringUtils.isBlank(runSeconds) ? 0 : Byte.valueOf(runSeconds)});
        // 自动模式下停止持续时间-分钟
        repeatParam.setVb502(new byte[]{StringUtils.isBlank(stopMinute) ? 0 : Byte.valueOf(stopMinute)});
        // 自动模式下停止持续时间-秒
        repeatParam.setVb503(new byte[]{StringUtils.isBlank(stopSeconds) ? 0 : Byte.valueOf(stopSeconds)});

        repeatParam.setV5100(map.get("repeatWeek0") == null ? false : true);
        repeatParam.setV5101(map.get("repeatWeek1") == null ? false : true);
        repeatParam.setV5102(map.get("repeatWeek2") == null ? false : true);
        repeatParam.setV5103(map.get("repeatWeek3") == null ? false : true);
        repeatParam.setV5104(map.get("repeatWeek4") == null ? false : true);
        repeatParam.setV5105(map.get("repeatWeek5") == null ? false : true);
        repeatParam.setV5106(map.get("repeatWeek6") == null ? false : true);
        repeatParam.setV5110(map.get("repeatTime1") == null ? false : true);
        repeatParam.setV5111(map.get("repeatTime2") == null ? false : true);
        repeatParam.setV5112(map.get("repeatTime3") == null ? false : true);
        repeatParam.setV5113(map.get("repeatTime4") == null ? false : true);
        keyList.forEach(key -> setProperties(map,key,repeatParam));
        repeatParam.getRealVal();
        return repeatParam;
    }

    public static String getFormatstr(int a, int b){
        return String.format("%d时%d分",a,b);
    }

    public static void setProperties(Map<String, String> map,String key,RepeatParam repeatParam){
        List<Integer> list = splitString(map.get(key));
        if (CollectionUtils.isEmpty(list)){
            return;
        }
        switch (key){
            case "startTime1":
                // 循环1：开始小时
                repeatParam.setVw512(list.get(0));
                // 循环1：开始分钟
                repeatParam.setVw528(list.get(1));
                break;
            case "endTime1":
                // 循环1：结束小时
                repeatParam.setVw514(list.get(0));
                // 循环1：结束分钟
                repeatParam.setVw530(list.get(1));
                break;
            case "startTime2":
                // 循环2：开始小时
                repeatParam.setVw516(list.get(0));
                // 循环2：开始分钟
                repeatParam.setVw532(list.get(1));
                break;
            case "endTime2":
                // 循环2：结束小时
                repeatParam.setVw518(list.get(0));
                // 循环2：结束分钟
                repeatParam.setVw534(list.get(1));
                break;
            case "startTime3":
                // 循环3：开始小时
                repeatParam.setVw520(list.get(0));
                // 循环3：开始分钟
                repeatParam.setVw536(list.get(1));
                break;
            case "endTime3":
                // 循环3：结束小时
                repeatParam.setVw522(list.get(0));
                // 循环3：结束分钟
                repeatParam.setVw538(list.get(1));
                break;
            case "startTime4":
                // 循环4：开始小时
                repeatParam.setVw524(list.get(0));
                // 循环4：开始分钟
                repeatParam.setVw540(list.get(1));
                break;
            case "endTime4":
                // 循环4：结束小时
                repeatParam.setVw526(list.get(0));
                // 循环4：结束分钟
                repeatParam.setVw542(list.get(1));
                break;

        }
    }

    public static List<Integer> splitString(String args) {
        if (StringUtils.isBlank(args)){
            return Collections.EMPTY_LIST;
        }
        List<Integer> list = new ArrayList<>();
        // 21点0分
        Pattern pattern = Pattern.compile("(\\d+)时(\\d+)分");
        Matcher matcher = pattern.matcher(args);
        if (matcher.find()) {
            String hour = matcher.group(1);
            String minutes = matcher.group(2);
            list.add(Integer.valueOf(hour));
            list.add(Integer.valueOf(minutes));
        }
        return list;

    }
}
